package com.example.crm.mapper.impl;

import com.example.crm.common.DBUtils;
import com.example.crm.entity.Customer;
import com.example.crm.mapper.CustomerMapper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

public class CustomerMapperImpl implements CustomerMapper {
    @Override
    public int addCustomer(Customer customer) throws SQLException {
        // 1. 连接数据库
        Connection conn = DBUtils.getConnection();

        // 2. 得到查询对象
        Statement stmt = conn.createStatement();

        PreparedStatement preparedStatement = null;

        String name = customer.getCust_name();
        String level = customer.getCust_level();
        String source = customer.getCust_source();
        String linkman = customer.getCust_linkman();
        String phone = customer.getCust_phone();
        String mobile = customer.getCust_mobile();

        // 3. 执行sql语句
        String sql = "insert into cst_customer(cust_name,cust_level,cust_source,cust_linkman,cust_phone,cust_mobile) values (?,?,?,?,?,?)";
        preparedStatement = conn.prepareStatement(sql);
        preparedStatement.setString(1, name);
        preparedStatement.setString(2, level);
        preparedStatement.setString(3, source);
        preparedStatement.setString(4, linkman);
        preparedStatement.setString(5, phone);
        preparedStatement.setString(6, mobile);

        int result = preparedStatement.executeUpdate();

        // 4. 得到结果集
        // 5. 返回结果
        if (result > 0) {
            DBUtils.destoryResource(conn, stmt, null, preparedStatement);
            return result;
        } else {
            DBUtils.destoryResource(conn, stmt, null, preparedStatement);
            return result;
        }
    }
}
